//
//  FFRecvPayVM.m
//  LLWallet
//
//  Created by tt on 2020/12/16.
//

#import "FFRecvPayVM.h"
#import "FFRecvPayCardCell.h"
#import "FFRecvPayItemCell.h"
#import "FFRecvVC.h"
#import "FFRecvPayCardOffCell.h"
#import "FFWalletManager.h"

@interface FFRecvPayVM ()


@end

@implementation FFRecvPayVM

-(NSArray<NSDictionary*>*) tableSectionMaps {
   
    NSMutableArray *items = [NSMutableArray array];
    __weak typeof(self) weakSelf = self;
    if(self.payQRCodeResp && self.payQRCodeResp.status == 1) {
        [items addObject:@{
            @"height":@(10.0f),
            @"items":@[
                    @{
                        @"class": FFRecvPayCardModel.class,
                        @"qrcode": weakSelf.payQRCodeResp.qrcode?:@"",
                        @"walletAmountFormat": weakSelf.payQRCodeResp.amountFormat?:@"",
                        @"onMore":^{
                                if(weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(ff_recvPayVMMorePressed:)]) {
                                    [weakSelf.delegate ff_recvPayVMMorePressed:weakSelf];
                                }
                        }
                    }
            ],
        }];
    }else{
        BOOL loading = false;
        if(!weakSelf.payQRCodeResp) {
            loading = true;
        }
        [items addObject:@{
            @"height":@(10.0f),
            @"items":@[
                    @{
                        @"class": FFRecvPayCardOffModel.class,
                        @"loading":@(loading),
                        @"onOpen":^{ // 开启
                            [weakSelf payOn];
                        }
                    }
            ],
        }];
    }
   
    
    [items addObject:@{
        @"height":@(20.0f),
        @"items":@[
                @{
                    @"class": FFRecvPayItemModel.class,
                    @"title":LLang(@"二维码收款"),
                    @"icon": [weakSelf imageName:@"recvmoney"],
                    @"onClick":^{
                        [[WKNavigationManager shared] pushViewController:[FFRecvVC new] animated:YES];
                    }
                }
        ],
    }];
    return items;
}

-(void) payOn {
    __weak typeof(self) weakSelf = self;
    [[FFWalletManager shared] ff_getPayToken:LLang(@"开启付款") remark:LLang(@"请输入支付密码，以验证身份") item:LIMPayItemFreePWDPayQRCodeOn callback:^(NSString * _Nonnull payToken) {
        [[WKNavigationManager shared].topViewController.view showHUD];
        [[WKAPIClient sharedClient] POST:@"impay/recvpay/pay/on" parameters:@{@"pay_token":payToken?:@""}].then(^{
            [[WKNavigationManager shared].topViewController.view hideHud];
            [weakSelf requestQRCode];
        }).catch(^(NSError *error){
            [[WKNavigationManager shared].topViewController.view switchHUDError:error.domain];
        });
    }];
   
}

-(AnyPromise*) requestQRCode {
    __weak typeof(self) weakSelf = self;
   return [[WKAPIClient sharedClient] POST:@"impay/recvpay/pay/qrcode" parameters:nil model:FFRecvPayQrcodeResp.class].then(^(FFRecvPayQrcodeResp *resp){
        weakSelf.payQRCodeResp = resp;
        [weakSelf reloadData];
    }).catch(^(NSError *error){
        [weakSelf reloadData];
    });
}

-(AnyPromise*) closeQRCode {
    __weak typeof(self) weakSelf = self;
   return [[WKAPIClient sharedClient] POST:@"impay/recvpay/pay/off" parameters:nil].then(^{
       weakSelf.payQRCodeResp = [FFRecvPayQrcodeResp new];
       weakSelf.payQRCodeResp.status = 0;
       [weakSelf reloadData];
    }).catch(^(NSError *error){
        [weakSelf reloadData];
    });
}



-(UIImage*) imageName:(NSString*)name {
    return [[WKApp shared] loadImage:name moduleID:@"LLWallet"];
}

@end

@implementation FFRecvPayQrcodeResp

+ (WKModel *)fromMap:(NSDictionary *)dictory type:(ModelMapType)type {
    FFRecvPayQrcodeResp *resp = [FFRecvPayQrcodeResp new];
    resp.status = dictory[@"status"]?[dictory[@"status"] integerValue]:0;
    if(dictory[@"data"]) {
        resp.amountFormat = dictory[@"data"][@"amount_format"];
        resp.qrcode = dictory[@"data"][@"qrcode"];
        resp.expire = dictory[@"data"][@"expire"]?[dictory[@"data"][@"expire"] integerValue]:60;
    }
    return resp;
}

@end
